﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using GISCommon.Entity;

namespace GISCommon.Tools
{
    public class MapOperatorTools
    {
        #region 画线
        /// <summary>
        /// 画线
        /// </summary>
        /// <param name="polyline">线对象</param>
        /// <param name="mapControl">地图控件</param>
        public static IElement DrawPolyline(IPolyline polyline, AxMapControl mapControl)
        {
            try
            {
                IRgbColor color = new RgbColorClass();
                color.Red = 255;

                ISimpleLineSymbol sym = new SimpleLineSymbolClass();
                sym.Color = color;
                sym.Style = esriSimpleLineStyle.esriSLSSolid;
                sym.Width = 4;

                ILineElement line = new LineElementClass();
                line.Symbol = sym;

                IElement element = (IElement)line;
                element.Geometry = polyline;

                if (mapControl == null) return null;

                mapControl.ActiveView.GraphicsContainer.AddElement(element, 0);
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, polyline.Envelope);

                return element;

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("DrawPolyLine(IPolyline polyline, AxMapControl mapControl)", ex);
            }
            return null;
        }
        public static IElement DrawPolylineWithoutReresh(IPolyline polyline, AxMapControl mapControl)
        {
            try
            {
                IRgbColor color = new RgbColorClass();
                color.Red = 255;

                ISimpleLineSymbol sym = new SimpleLineSymbolClass();
                sym.Color = color;
                sym.Style = esriSimpleLineStyle.esriSLSSolid;
                sym.Width = 4;

                ILineElement line = new LineElementClass();
                line.Symbol = sym;

                IElement element = (IElement)line;
                element.Geometry = polyline;

                if (mapControl == null) return null;

                mapControl.ActiveView.GraphicsContainer.AddElement(element, 0);                

                return element;

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("DrawPolyLine(IPolyline polyline, AxMapControl mapControl)", ex);
            }
            return null;
        }
        /// <summary>
        /// 画线
        /// </summary>
        /// <param name="polyline">线对象</param>
        /// <param name="mapControl">地图控件</param>
        /// <param name="color">颜色</param>
        public static void DrawPolyline(IPolyline polyline, AxMapControl mapControl,IRgbColor color )
        {
            try
            {
                ISimpleLineSymbol sym = new SimpleLineSymbolClass();
                sym.Color = color;
                sym.Style = esriSimpleLineStyle.esriSLSSolid;
                sym.Width = 2;

                ILineElement line = new LineElementClass();
                line.Symbol = sym;

                IElement element = (IElement)line;
                element.Geometry = polyline;

                if (mapControl == null) return;

                mapControl.ActiveView.GraphicsContainer.AddElement(element, 0);
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, polyline.Envelope);

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("DrawPolyLine(IPolyline polyline, AxMapControl mapControl)", ex);
            }
        }
        /// <summary>
        /// 画线
        /// </summary>
        /// <param name="etys">坐标点集</param>
        /// <param name="mapControl">地图控件</param>
        public static void DrawPolyline(Coordinate_Ety[] etys,AxMapControl mapControl,int width)
        {
            try
            {
                if (etys == null) return;

                IPointCollection pointColl = new PolylineClass();
                object obj = Missing.Value;
                foreach (Coordinate_Ety ety in etys)
                {
                    IPoint point = new PointClass();
                    point.X = ety.LONGITUDE;
                    point.Y = ety.LATITUDE;

                    pointColl.AddPoint(point,ref obj,ref obj);
                }

                if (pointColl.PointCount == 0) return;

                IPolyline polyline = (IPolyline) pointColl;

                IRgbColor color = new RgbColorClass();
                color.Red = 0;
                color.Blue = 255;

                ISimpleLineSymbol sym = new SimpleLineSymbolClass();
                sym.Color = color;
                sym.Style = esriSimpleLineStyle.esriSLSSolid;
                sym.Width = width;

                ILineElement line = new LineElementClass();
                line.Symbol = sym;

                IElement element = (IElement) line;
                element.Geometry = polyline;

                if (mapControl == null) return;

                mapControl.ActiveView.GraphicsContainer.AddElement(element,0);
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, polyline.Envelope);

            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPolyline(Coordinate_Ety[] etys,AxMapControl mapControl)出错！",ex);
            }
        }
        #endregion

        #region 画点
        /// <summary>
        /// 画点
        /// </summary>
        /// <param name="drawPoint">点对象</param>
        /// <param name="mapControl">地图控件</param>
        public static IElement DrawPoint(IPoint drawPoint,AxMapControl mapControl)
        {
            try
            {
                //IDisplay display = mapControl.ActiveView.ScreenDisplay;
                IMarkerSymbol symbol = new SimpleMarkerSymbolClass();
                IRgbColor color = new RgbColorClass();
                color.Blue = 255;
                symbol.Color = color;
                symbol.Size = 8;

                //ISymbol sym = (ISymbol)symbol;
                //display.SetSymbol(sym);

                //display.StartDrawing(0,0);
                //display.DrawPoint(drawPoint);
                //display.FinishDrawing();
                IMarkerElement mark = new MarkerElementClass();
                mark.Symbol = symbol;
                IElement element = (IElement) mark;
                element.Geometry = drawPoint;

                mapControl.ActiveView.GraphicsContainer.AddElement(element, 0);
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, null);

                return element;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPoint(IPoint drawPoint,AxMapControl mapControl)出错！", ex);
            }

            return null;
        }

        /// <summary>
        /// 画点
        /// </summary>
        /// <param name="drawPoint">点对象</param>
        /// <param name="mapControl">地图控件</param>
        /// <param name="color">颜色</param>
        /// <returns></returns>
        public static IElement DrawPoint(IPoint drawPoint, AxMapControl mapControl,IRgbColor color)
        {
            try
            {
                //IDisplay display = mapControl.ActiveView.ScreenDisplay;
                IMarkerSymbol symbol = new SimpleMarkerSymbolClass();

                symbol.Color = color;
                symbol.Size = 8;

                //ISymbol sym = (ISymbol)symbol;
                //display.SetSymbol(sym);

                //display.StartDrawing(0,0);
                //display.DrawPoint(drawPoint);
                //display.FinishDrawing();
                IMarkerElement mark = new MarkerElementClass();
                mark.Symbol = symbol;
                IElement element = (IElement)mark;
                element.Geometry = drawPoint;

                mapControl.ActiveView.GraphicsContainer.AddElement(element, 0);
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, null);

                return element;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPoint(IPoint drawPoint,AxMapControl mapControl)出错！", ex);
            }

            return null;
        }
        #endregion
    }
}
